package com.andnnl.cfop_rand

import android.os.Build
import android.os.Bundle
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.ComponentActivity
import androidx.webkit.WebViewAssetLoader
import java.io.InputStream


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 定义自定义域名
        val customDomain = "myappassets.com"
        // 初始化 WebViewAssetLoader
        val assetLoader = WebViewAssetLoader.Builder()
//            .setDomain(customDomain)
            .addPathHandler("/assets/", WebViewAssetLoader.AssetsPathHandler(this))
            .build()
        val webView = WebView(this).apply {
            settings.apply {
                javaScriptEnabled = true
                domStorageEnabled = true  // 启用 localStorage
                allowFileAccess = true    // 允许文件访问
                allowContentAccess = true // 允许加载 content://
                mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            }
//            webViewClient = WebViewClient() // 防止跳转到浏览器

            // 自定义 WebViewClient 并使用 WebViewAssetLoader
            webViewClient = object : WebViewClient() {
                override fun shouldInterceptRequest(
                    view: WebView?,
                    request: WebResourceRequest
                ): android.webkit.WebResourceResponse? {
                    return assetLoader.shouldInterceptRequest(request.url)
                }
            }
//            loadUrl("file:///android_asset/index.html")
            // 修改加载的 HTML 页面 URL 为使用 WebViewAssetLoader 的格式
            loadUrl("https://appassets.androidplatform.net/assets/index.html")
//            loadUrl("https://$customDomain/assets/index.html")

        }
        setContentView(webView)
    }

    // 加载JSON文件
    private fun loadJsonFromAssets(fileName: String): WebResourceResponse? {
        return try {
            val inputStream: InputStream = assets.open(fileName)
            WebResourceResponse("application/json", "UTF-8", inputStream)
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }

    // 加载其他资源文件
    private fun getWebResourceResponse(assetPath: String): WebResourceResponse? {
        return try {
            val mimeType = when {
                assetPath.endsWith(".js") -> "text/javascript"
                assetPath.endsWith(".css") -> "text/css"
                assetPath.endsWith(".png") -> "image/png"
                assetPath.endsWith(".jpg", ignoreCase = true) -> "image/jpeg"
                assetPath.endsWith(".json") -> "application/json"
                else -> "text/html"
            }

            val inputStream: InputStream = assets.open(assetPath)
            WebResourceResponse(mimeType, "UTF-8", inputStream)
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }
}

/*
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    Cfop_randTheme {
        Greeting("Android")
    }
}*/
